package com.forum.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.forum.annotation.IgnoreAuth;
import com.forum.entity.UserTabEntity;
import com.forum.service.TokenService;
import com.forum.service.UserTabService;
import com.forum.utils.MPUtil;
import com.forum.utils.PageUtils;
import com.forum.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassNameUserTabController
 * @Author Lenovo
 * @Date 2023/11/13 17:08
 */

@RestController
@RequestMapping("/users")
@Slf4j
@Api(tags = "用户控制")
public class UserTabController {
    @Autowired
    private UserTabService userTabService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TokenService tokenService;

    @IgnoreAuth
    @PostMapping("/registerUser")
    @ApiOperation(value = "注册",notes = "")
    public R register(@RequestBody UserTabEntity user_tab){
        //判断是否存在该用户，根据用户名进行判断
        if (userTabService.selectOne(new EntityWrapper<UserTabEntity>().eq("UserName",user_tab.getUserName()))!=null){
            return R.error("用户已存在！");
        }
        //新增单体用户实体进入数据库
        userTabService.insert(user_tab);
        return R.ok("注册成功！");
    }

    /**
     *@Author zhangjc2
     *
     *@Description // 登录逻辑
     *@Date 2023-12-01 22:18
     * @param password
     * @param PhoneNum
     * @param PhoneCode
     * @return com.forum.utils.R
     **/
    @IgnoreAuth
    @PostMapping("/login")
    @ApiOperation(value = "登录",notes = "")
    public R login(String password, String PhoneNum, Integer PhoneCode) {
        Integer CodeInRedis = (Integer) redisTemplate.opsForValue().get(PhoneNum);
        if (CodeInRedis !=null && CodeInRedis.equals(PhoneCode)) {

            UserTabEntity User = userTabService.selectOne(new EntityWrapper<UserTabEntity>().eq("PhoneNum", PhoneNum));
            if (User == null) {
                return R.error("账号不存在！");
            }
            //开发过程使用
            String userPassword = User.getPassword();

            String userName = User.getUserName();
            log.info("当前用户账号为:{}"+"当前用户密码为:{}",userName,userPassword);

            //
            if (!User.getPassword().equals(password)) {
                return R.error("账号或者密码错误！");
            }
            /*if (!"是".equals(StudentEntity.getToExmine())) {
                return R.error("账号状态异常，请联系管理员");
            }*/
            // 获取管理员的token信息
            String token = tokenService.generateToken(User.getId(), userName, "usertab", User.getRole());
            return R.ok().put("token", token);
        } else {
            return R.error("验证码输入不正确请再次尝试");
        }
    }

    /**
     * 获取用户的session用户信息
     */
    @GetMapping("/session")
    public R getCurrUser(HttpServletRequest request){
        Long id = (Long)request.getSession().getAttribute("userId");
        UserTabEntity user = userTabService.selectById(id);
        return R.ok().put("data", user);
    }

    @IgnoreAuth
    @GetMapping("/getAllUser")
    @SuppressWarnings("unchecked")
    public R getAllUser(@RequestParam Map <String, Object> params, UserTabEntity user){
        EntityWrapper<UserTabEntity> userQueryWrapper = new EntityWrapper<>();
        PageUtils userTabEntityPage = userTabService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(userQueryWrapper, user), params), params));
        return R.ok().put("data",userTabEntityPage);
    }

    @IgnoreAuth
    @GetMapping("/getList")
    @SuppressWarnings("unchecked")
    public R list(UserTabEntity userTabEntity){
        EntityWrapper<UserTabEntity> ew = new EntityWrapper<>();
        ew.allEq(MPUtil.allEQMapPre( userTabEntity, "user"));
        return R.ok().put("data",userTabService.selectListView(ew));
    }

}
